import { SuccessAjax,Storage } from "hongshu-view";
import { ref} from "vue";
import api from "~api";
import EventBus from "~/tools/EventBus.js";
import {UpdateAuthByMenu} from "./AuthTool";
import {LocaleMenuData} from "./LocaleTool";

const menuList = ref([]);   //菜单列表
var router = null;


//加载菜单需要的话请重构
const loadMenu = (moduleId) => {    
    if(__MODEL__ === "publish"){
        moduleId = window.sessionStorage.getItem("moduleId");
    }else if(__MODEL__ == "development"){
       let logininfo = Storage.get("logininfo");
       if(!logininfo){
        ElMessageBox.alert('开发模式请手动输入logininfo', '提醒', {
            callback: (action) => {
  
            },
          });
          return;
       };
    };
    if(!moduleId){
        console.warn("无法获取 moduleId");
        return;
    }else{
        SuccessAjax(api.BaseApiPath+"system/hscomm/findAllTillAuth?moduleId="+moduleId, {}, {
            method: "get",
            errorMsg:false,
            handleError:false,
            loading:false,
        }).then(({ data }) => {
            LocaleMenuData(data);
            menuList.value = data;
            UpdateAuthByMenu(data);
            EventBus.emit("ROUTER-UPDATE");
    
          
        }).catch((error)=>{
            console.error(error)
        });
    }
   
};



const tapMenu = (info)=>{
    EventBus.emit("ROUTER-RESET", {
        path:"/"+info.url
    });
    router.push({
        path:"/"+info.url
    });

}


export default (r)=>{   
    router = r;
    return {
        loadMenu,
        menuList,
        tapMenu,
    };
};